package com.sunday.volunteer.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;


import com.sunday.volunteer.R;
import com.sunday.volunteer.entity.Address;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Pual's PC on 2016/10/9.
 */
public class AddressAdapter extends RecyclerViewAdapter {

    public AddressAdapter(List list, int layout) {
        super(list, layout);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(View itemView, int viewType) {
        return new ViewHolder(itemView);
    }




    public class ViewHolder extends MyViewHolder{
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.address)
        TextView address;
        @Bind(R.id.address_edit)
        TextView address_edit;
        @Bind(R.id.address_del)
        TextView address_del;
        @Bind(R.id.to_address)
        View to_address;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this , itemView);
            address_edit.setOnClickListener(onClickListener);
            address_del.setOnClickListener(onClickListener);
            to_address.setOnClickListener(onClickListener);
        }

        @Override
        public void bind(MyViewHolder holder, Object object, int position) {
            ViewHolder viewHolder = (ViewHolder) holder;
            Address item = (Address)object;
            address_edit.setTag(position);
            address_del.setTag(position);
            to_address.setTag(position);
            if(item == null){
                return;
            }
            setText(name , String.format("%s%s" ,item.getReceiveName() ,"  ",mobileInvisible(item.getReceiveMobile())) );
            setText(address ,String.format("%s" , item.getReceiveAddress() ));
        }
    }

    private String mobileInvisible(String mobile){
        String text = "";
        if(TextUtils.isEmpty(mobile)){
            text = "****";
        }else {
            if(mobile.length() >= 4){
                text = String.format("%s%s%s" , mobile.substring(0,3) , "****" , mobile.substring(mobile.length() -4 , mobile.length()));
            }else {
                text = "****";
            }
        }
        return text;
    }
}
